// pages/login/login.js
const API = require('../../utils/api')
const Base = require('../../utils/base')
const Auth = require('../../utils/auth')
// 全局变量
let app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    jscode:null, // 微信的登录code
    phonenumber:null, // 手机号码
    phonecode:null, // 验证码
    mhide:false,
    thide:true,
    time:10,
    lbcolor:'#EBEBEB',
    tbcolor:'#EBEBEB',
    message:'获取验证码'
  },
  /**
   * 改变登录按钮颜色
   * 验证码输入监听
   */
  changelogincolor:function(){
    console.log('改变登录按钮颜色->', this.data.phonenumber)
    this.setData({
      lbcolor: '#9ac81f'
    })
  },
  /**
   * 获取手机验证码(调用第三方)
   */
  sendverifycode:function(){
    let that = this;
    var phonenumber = parseInt(this.data.phonenumber)
    console.log('获取手机验证码(调用第三方)', phonenumber)
    let params = {
        'phoneNumber': phonenumber
        }
    API.getVerifyCode(params).then(res => {
      // 验证码
      // app.globalData.verifyCode = res.data;
      this.setData({
        phonecode : res.data,
        lbcolor: '#9ac81f', // 更新登录按钮颜色
        mhide: false, // 取消倒计时
        thide: true, // 取消倒计时
      })
      console.log('Login->getVerifyCode->', this.data.phonecode)
    })
    .catch(err => {
      console.log(err)
    })
  },
  /**
   * 更新‘获取验证码’按钮
   * 手机号码输入监听
   * @param {*} e 
   */
changecolor:function(e){
if(e.detail.value.length==11)
{
  console.log('手机号码输入监听->', e.detail.value)
  this.setData({
    tbcolor: '#9ac81f',
    phonenumber: e.detail.value
  })
}
},
timecount:function(){
  if (this.data.tbcolor == '#9ac81f')
  {
    this.sendverifycode();
    this.countdown();
  this.setData({
    mhide: true,
    thide: false,
    tbcolor: '#EBEBEB',
  })
 
  }
},
countdown: function () {
  var that = this
  var second = that.data.time
   console.log(second)
  if (second == 0) {
    console.log("Time Out...");
    that.setData({
      mhide: false,
      thide: true,
    });
    return;
  }
  var timecount = setTimeout(function () {
    that.setData({
      time: second - 1
    });
    that.countdown();
  }
    , 1000)
},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      var user = wx.getStorageSync('user');
      var openid = wx.getStorageSync('openid');
      if (!openid){
        API.login()
      }else if (!user){
        API.getUserInfo()
      }else{
        console.log('用户已登录')
      }
      // 跳转打车页面
      wx.redirectTo({
        url: '/pages/route/special',
      })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
   
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  /**
   * 叫车
   */
  changetopage:function(){
    wx.redirectTo({
      url: '../route/special',
    })
  },
  /**
   * 注册
   */
  sendtosignin:function(){
    wx.navigateTo({
      url: 'signin/signin',
    })
  },
  /**
   * 登录
   */
  sendMpLogin:function(){
   console.log('登录.........')
  }
})